#!/bin/bash
# 全面清理Linux防火墙并启动向日葵容器
# 适用于Debian、Ubuntu、CentOS等主流Linux发行版
# 需要root权限运行
# 使用方法：
# curl -sSL https://gitee.com/edclol/sh/raw/master/k.sh | sudo bash
# sudo bash <(curl -sSL https://gitee.com/edclol/sh/raw/master/k.sh)
set +e
trap 'echo "Command: $BASH_COMMAND" >&2' ERR

echo "开始全面清理防火墙..."

# 处理ufw防火墙
echo "处理ufw防火墙..."
sudo ufw --force reset 2>/dev/null
sudo ufw --force disable 2>/dev/null
sudo systemctl stop ufw 2>/dev/null
sudo systemctl disable ufw 2>/dev/null
sudo systemctl mask ufw 2>/dev/null || true
# 清理ufw配置文件
sudo rm -f /etc/ufw/*.rules 2>/dev/null

# 处理firewalld防火墙
echo "处理firewalld防火墙..."
sudo systemctl stop firewalld 2>/dev/null
sudo systemctl disable firewalld 2>/dev/null
sudo firewall-cmd --complete-reload 2>/dev/null || true
sudo systemctl mask firewalld 2>/dev/null || true
# 清理firewalld配置
sudo rm -rf /etc/firewalld/zones/* 2>/dev/null

# 处理nftables防火墙 (较新系统使用)
echo "处理nftables防火墙..."
sudo systemctl stop nftables 2>/dev/null
sudo systemctl disable nftables 2>/dev/null
sudo nft flush ruleset 2>/dev/null || true  # 清空所有nftables规则
sudo systemctl mask nftables 2>/dev/null || true
# 清理nftables配置文件
sudo rm -f /etc/nftables.conf 2>/dev/null
sudo rm -rf /etc/nftables.d 2>/dev/null

# 处理iptables-services (某些系统专用的iptables服务)
echo "处理iptables-services..."
sudo systemctl stop iptables 2>/dev/null
sudo systemctl stop ip6tables 2>/dev/null
sudo systemctl disable iptables 2>/dev/null
sudo systemctl disable ip6tables 2>/dev/null
sudo systemctl mask iptables 2>/dev/null || true
sudo systemctl mask ip6tables 2>/dev/null || true

# 处理shorewall防火墙
echo "处理shorewall防火墙..."
sudo systemctl stop shorewall 2>/dev/null
sudo systemctl disable shorewall 2>/dev/null
sudo shorewall clear 2>/dev/null || true  # 清空shorewall规则
sudo systemctl mask shorewall 2>/dev/null || true

# 处理ebtables (用于桥接网络的防火墙)
echo "处理ebtables防火墙..."
sudo systemctl stop ebtables 2>/dev/null
sudo systemctl disable ebtables 2>/dev/null
sudo ebtables -F 2>/dev/null
sudo ebtables -X 2>/dev/null
sudo systemctl mask ebtables 2>/dev/null || true

# 处理iptables规则
echo "清除iptables规则..."
# 清除filter表规则
sudo iptables -F 2>/dev/null
sudo iptables -X 2>/dev/null
sudo iptables -Z 2>/dev/null  # 清零计数器
sudo iptables -P INPUT ACCEPT 2>/dev/null
sudo iptables -P OUTPUT ACCEPT 2>/dev/null
sudo iptables -P FORWARD ACCEPT 2>/dev/null

# 清除nat表规则
sudo iptables -t nat -F 2>/dev/null
sudo iptables -t nat -X 2>/dev/null
sudo iptables -t nat -Z 2>/dev/null

# 清除mangle表规则
sudo iptables -t mangle -F 2>/dev/null
sudo iptables -t mangle -X 2>/dev/null
sudo iptables -t mangle -Z 2>/dev/null

# 清除raw表规则
sudo iptables -t raw -F 2>/dev/null
sudo iptables -t raw -X 2>/dev/null
sudo iptables -t raw -Z 2>/dev/null

# 清除ip6tables规则
echo "清除ip6tables规则..."
sudo ip6tables -F 2>/dev/null
sudo ip6tables -X 2>/dev/null
sudo ip6tables -Z 2>/dev/null
sudo ip6tables -P INPUT ACCEPT 2>/dev/null
sudo ip6tables -P OUTPUT ACCEPT 2>/dev/null
sudo ip6tables -P FORWARD ACCEPT 2>/dev/null

# 清除ip6tables的nat、mangle和raw表
sudo ip6tables -t nat -F 2>/dev/null
sudo ip6tables -t nat -X 2>/dev/null
sudo ip6tables -t nat -Z 2>/dev/null
sudo ip6tables -t mangle -F 2>/dev/null
sudo ip6tables -t mangle -X 2>/dev/null
sudo ip6tables -t mangle -Z 2>/dev/null
sudo ip6tables -t raw -F 2>/dev/null
sudo ip6tables -t raw -X 2>/dev/null
sudo ip6tables -t raw -Z 2>/dev/null

# 保存配置（如果系统支持）
echo "保存防火墙配置..."
sudo iptables-save >/dev/null 2>&1 || true
sudo ip6tables-save >/dev/null 2>&1 || true
sudo service iptables save 2>/dev/null || true

# 运行向日葵容器
echo "启动向日葵容器..."
sudo docker rm -f sunlogin 2>/dev/null || true
sudo docker run -d --name sunlogin --restart=always --network host registry.cn-hangzhou.aliyuncs.com/bdfzfx/sunlogin:knt-laptop 2>/dev/null || {
    echo "....." >&2
}
echo "查询向日葵容器状态..."
sudo docker ps -a --filter "name=sunlogin"

echo "所有操作已尝试执行完毕"
